<template>
  <ul class="contextmenu">
    <li v-if="status.item ==='other'" @click="refresh">
      <span class="my-cloud_disk-refresh"></span>刷新</li>
    <li v-if="status.item=== 'file'" @click="downloadFile">
      <span class="my-cloud_disk-download"></span>下载</li>
    <li v-if="status.item==='folder'||status.item==='file'" @click="deleteFile">
      <span class="my-cloud_disk-delete"></span>删除</li>
    <li v-if="status.item==='folder'||status.item==='file'">
      <span class="my-cloud_disk-rename"></span>重命名</li>
    <li v-if="status.item==='folder'||status.item==='file'">
      <span class="my-cloud_disk-copy"></span>拷贝</li>
    <li v-if="status.item==='folder'||status.item==='file'">
      <span class="my-cloud_disk-cut"></span>剪切</li>
    <li v-if="status.item==='other'">
      <span class="my-cloud_disk-paste"></span>粘贴</li>
    <li v-if="status.item==='other'" @click="returnEvent('uploadFile')">
      <span class="my-cloud_disk-upload"></span>上传文件</li>
    <li v-if="status.item==='other'" @click="returnEvent('createFolder')">
      <span class="my-cloud_disk-folder"></span>新建文件夹</li>
  </ul>
</template>

<script>
import api from "../../apis/api";
import {baseUrl} from "../../apis/requests/requests";

export default {
  name: "MenuItem",
  props: ['status'],
  methods:{
    refresh(){
      this.$router.go(0)
    },
    returnEvent(event){
      this.$emit("events",event)
    },
    downloadFile(){
      // window.location.href = `${baseUrl}/file/downLoadFile?fileId=${this.status.Id}`
      let that = this
      api.downloadFile(that.status.Id).then(response => {
        console.log(response)
        if (!response) {
          that.$util.errorMessage("资源获取失败")
          return false;
        }
        if(that.$util.axiosDownload(response)){
          that.$util.successMessage("下载成功")
        }else{
          that.$util.errorMessage("下载失败")
        }
      })
    },
    deleteFile(){
      let that = this;
      if (that.status.item==='folder'){
        // 删除文件夹
        api.deleteFolder(this.status.Id).then(response => {
          if (response.code===200){
            let data = {
              type:'folder',
              id:that.status.Id
            }
            this.$util.successMessage(response.message);
            this.$emit("deleteEvent",data)
            return 0;
          }
          this.$util.errorMessage(response.message);

        })
      }else {
        // 删除文件
        api.deleteFile(this.status.Id).then(response => {
          if (response.code===200){
            let data = {
              type:'file',
              id:that.status.Id
            }
            this.$util.successMessage(response.message);
            this.$emit("deleteEvent",data)
            return 0;
          }
          this.$util.errorMessage(response.message);
        })
      }
    }
  }
}
</script>

